from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class StockPicking(models.Model):
    _inherit = 'stock.picking'

    is_lock = fields.Boolean(string='Lock or Unlock', copy=False)
    lock_reason = fields.Html(string='Lock Reason', copy=False)
    active = fields.Boolean(default=True)

    def button_validate(self):
        for rec in self:
            if rec.is_lock and not rec._context.get('ecr_apply', False):
                raise ValidationError('Sale order is lock, please unlock')
        return super().button_validate()

    def action_cancel(self):
        for rec in self:
            if rec.is_lock and not rec._context.get('ecr_apply', False):
                raise ValidationError('Sale order is lock, please unlock')
        return super().action_cancel()
